<?php
/**
 * Created by PhpStorm.
 * User: 田家强
 * Date: 2017/3/26
 * Time: 12:34
 */

namespace Admin\Controller;



use Common\Controller\BaseController;
use Common\Model\GoodsListModel;
use Think\Controller;

class GoodsListController  extends  BaseController
{

    public function index()
    {
        $gid = I('get.gid');
        if(IS_POST)
        {
            $data = I('post.');
            //将数组转成字符串
            $combine= implode(',',$data['combine']);
            $data=[
               'number' => $data['number'],
               'inventory'=>$data['inventory'],
               'gid'=>$gid,
               'combine'=>$combine,
            ];
//        $res=   m('goods_list')->add($data);
            //dd($res);die;
       $this->store(new GoodsListModel(),$data);
            //dd($data);
            //dd($data['combine']);
            //dd($combine);
        }
          //类型属性、商品属性表关联
          //dd($gid);
        $field = m('typeattr')->alias('ta')
            ->join("__GOODS_ATTR__ ga on ta.taid = ga.taid")
           //查询指定数据
            ->where("gid={$gid} and ta.value=0")->select();
        //dd($field);
       //设置一个空数组用来接收数据
        $data =[];
        foreach($field as $k=>$v)
        {
           $data[$v['name']][] =$v;

        }

      $goods = m('goods_list')->where("gid=$gid")->select();
     //dd($goods);
        foreach( $goods as $kk=>$vv)
        {
           //$re =  explode(',', $dd);
            $w=[
                'gtid'=>array('in',$vv['combine'])
            ];
            // $map['id']  = array('not in','1,5,8');
            $goods[$kk]['combine']   =m('goods_attr')->where($w)->getField('gtvalue',true);
            //dd( $goods[$kk]['combine']);
        }

        $this->assign('goods',$goods);
        $this->assign('data',$data);
        //dd($data);
        //dd($goods);
        $this->display();
    }
    public function del()
    {
        $tgid = I('get.tgid');
        m('goods_list')->where("tgid={$tgid}")->delete();
        $this->success('删除成功',u('admin/goodsList/index',['gid'=> $_GET['gid']]));


    }

}